clear
set more off

local inputs_dir "C:\Users\yu_xu\Dropbox\Bond.Risk.Premia.Mitra.Xu\Data\combined_results_yu\data"
local outputs_dir "C:\Users\yu_xu\Dropbox\Bond.Risk.Premia.Mitra.Xu\Data\results_writeup_Dec2019"

local SAMPLE_START_YEAR 1964
local SAMPLE_END_YEAR 2016

local NEWEY_WEST_LAGS 12

* Fama-Bliss ZCB prices

use "`inputs_dir'\fama_bliss_prices.dta"
* format: Date_yyyymmdd (end of month price), price_1y, ..., price_5y
* note: prices are for a face value of 100

* convert date to datem
gen D = mod(Date_yyyymmdd,100)
gen M = mod((Date_yyyymmdd-D)/100,100)
gen Y = (Date_yyyymmdd - 100*M - D)/10000
gen datem = ym(Y,M)
format datem %tm

keep datem price_1y price_2y price_3y price_4y price_5y
order datem price_1y price_2y price_3y price_4y price_5y

* nonfarm employees
merge 1:1 datem using "`inputs_dir'\PAYEMS.dta"
drop _merge

* merge Fed Funds Rate
merge 1:1 datem using "`inputs_dir'\FEDFUNDS_monthly.dta"
drop _merge

* merge CPI index
merge 1:1 datem using "`inputs_dir'\CPIAUCSL.dta"
drop _merge

* merge in greenwood and vayanos variable MWDGDP
merge 1:1 datem using "`inputs_dir'\greenwood_vayanos.dta"
drop _merge

* labor market variables
merge 1:1 datem using "`inputs_dir'\unrate_sa.dta"
drop _merge

merge 1:1 datem using "`inputs_dir'\HWI_barnichon.dta"
drop _merge

merge 1:1 datem using "`inputs_dir'\unemp_flow_rates.dta"
drop _merge

tsset datem

// 12 month holding period excess returns
// timing: realized holding period returns on the date
foreach n of numlist 2/5{
	local lag_n = `n' - 1
	gen exret_`n'y = 100*(log(price_`lag_n'y/100) - log(L12.price_`n'y/100) + log(L12.price_1y/100))
}

gen ave_exret = (exret_2y + exret_3y + exret_4y + exret_5y)/4
gen dlogN1m = 100*(log(PAYEMS) - log(L.PAYEMS))

gen INF = 100*(log(CPIAUCSL) - log(L.CPIAUCSL))

gen tightness = HWI/unrate

* summary stats
local sumvarlist ave_exret exret_2y exret_3y exret_4y exret_5y dlogN1m INF FEDFUNDS MWDGDP tightness f s

eststo clear
estpost summarize `sumvarlist' if datem>=ym(`SAMPLE_START_YEAR',1) & datem<=ym(`SAMPLE_END_YEAR',12), detail
esttab using "`outputs_dir'\summary_stats_nominal_search.csv", replace cells("count mean sd min p25 p50 p75 max") nomtitle nonumber
eststo clear

local rhs_list tightness f s
local lhs_list ave_exret exret_2y exret_3y exret_4y exret_5y

foreach rhs of local rhs_list{

	eststo clear
	foreach lhs of local lhs_list{
		qui regress `lhs' L12.`rhs' if datem>=ym(`SAMPLE_START_YEAR',1) & datem<=ym(`SAMPLE_END_YEAR',12)
		local r2_temp = round(`e(r2)',.001)
		eststo: qui newey `lhs' L12.`rhs' if datem>=ym(`SAMPLE_START_YEAR',1) & datem<=ym(`SAMPLE_END_YEAR',12), lag(`NEWEY_WEST_LAGS')
		qui estadd local sample_year "`SAMPLE_START_YEAR'-`SAMPLE_END_YEAR'"
		qui estadd local display_NWlags "`NEWEY_WEST_LAGS'"
		qui estadd local r_squared "`r2_temp'"
	}
	esttab using "`outputs_dir'\univariate_`rhs'_pred_nominal.csv", replace b(2) t(2) r2(3) scalars("sample_year Sample" "display_NWlags NW-lags" "r_squared R2") star(+ 0.10 * 0.05 ** 0.01 *** 0.001)
	esttab using "`outputs_dir'\univariate_`rhs'_pred_nominal.tex", replace b(2) t(2) r2(3) scalars("sample_year Sample" "display_NWlags NW-lags" "r_squared R2") star(+ 0.10 * 0.05 ** 0.01 *** 0.001)
	eststo clear
	
	eststo clear
	foreach lhs of local lhs_list{
		qui regress `lhs' L12.`rhs' L12.FEDFUNDS L12.INF L12.MWDGDP if datem>=ym(`SAMPLE_START_YEAR',1) & datem<=ym(`SAMPLE_END_YEAR',12)
		local r2_temp = round(`e(r2)',.001)
		eststo: qui newey `lhs' L12.`rhs' L12.FEDFUNDS L12.INF L12.MWDGDP if datem>=ym(`SAMPLE_START_YEAR',1) & datem<=ym(`SAMPLE_END_YEAR',12), lag(`NEWEY_WEST_LAGS')
		qui estadd local sample_year "`SAMPLE_START_YEAR'-`SAMPLE_END_YEAR'"
		qui estadd local display_NWlags "`NEWEY_WEST_LAGS'"
		qui estadd local r_squared "`r2_temp'"
	}
	esttab using "`outputs_dir'\multivar_`rhs'_pred_nominal.csv", replace b(2) t(2) r2(3) scalars("sample_year Sample" "display_NWlags NW-lags" "r_squared R2") star(+ 0.10 * 0.05 ** 0.01 *** 0.001)
	esttab using "`outputs_dir'\multivar_`rhs'_pred_nominal.tex", replace b(2) t(2) r2(3) scalars("sample_year Sample" "display_NWlags NW-lags" "r_squared R2") star(+ 0.10 * 0.05 ** 0.01 *** 0.001)
	eststo clear
}
